<?php

namespace app\admin\controller\depot;

use app\common\controller\Backend;

class Supplier extends Backend
{
    // 设置关联查询
    protected $relationSearch = true;

    // 当前模型
    protected $model = null;

    // 无需登陆
    protected $noNeedLogin = [];
    // 无需鉴权
    protected $noNeedRight = [];

    public function __construct(){
        parent::__construct();

        $this->model = model('Depot.Supplier');
        $this->RegionModel = model('Region');
    }

    public function index(){
        $this->request->filter(['strip_tags','trim']);

        if($this->request->isAjax()){
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort,$order)
                ->count();
                
            $list = $this->model
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            // 组装数据
            $result = [
                'total' => $total,
                'rows' => $list
            ];

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add(){
        if($this->request->isPost()){
            // var_dump($this->request->param());
            // exit;
            $params = $this->request->param('row/a');

            // 组装数据
            $data = [
                'name' => $params['name'],
                'mobile' => $params['mobile'],
                'address' => $params['address']
            ];

            // 选择地区
            if(!empty($params['code'])){
                $parentpath = $this->RegionModel->where(['code'=>$params['code']])->value('parentpath');

                // var_dump($params['code']);
                // exit;
                if(!$parentpath){
                    $this->error('该地区不存在，请重新选择');
                    exit;
                }

                $path = explode(',',$parentpath);
                $province = $path[0] ?? null;
                $city = $path[1] ?? null;
                $district = $path[2] ?? null;

                $data['province'] = $province;
                $data['city'] = $city;
                $data['district'] = $district;
            }

            // var_dump($data);
            // exit;

            $result = $this->model->validate('common/Depot/Supplier')->save($data);
            if($result === FALSE){
                $this->error('添加供应商失败');
                exit;
            }else{
                $this->success('添加供应商成功');
                exit;
            }
        }

        return $this->view->fetch();
    }

    public function edit($ids=NULL){
        $row = $this->model->find($ids);

        // 没有数据
        if(!$row){
            $this->error(__('No Results were found'));
            exit;
        }

        if($this->request->isPost()){
            $params = $this->request->param('row/a');

            $data = [
                'id' => $ids,
                'name' => $params['name'],
                'mobile' => $params['mobile'],
                'address' => $params['address']
            ]; 

            if(!empty($params['code'])){
                $parentpath = $this->RegionModel->where(['code'=>$params['code']])->value('parentpath');

                // var_dump($params['code']);
                // exit;
                if(!$parentpath){
                    $this->error('该地区不存在，请重新选择');
                    exit;
                }

                $path = explode(',',$parentpath);
                $province = $path[0] ?? null;
                $city = $path[1] ?? null;
                $district = $path[2] ?? null;

                $data['province'] = $province;
                $data['city'] = $city;
                $data['district'] = $district;
            }

            $result = $this->model->validate('common/Depot/Supplier')->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error($this->error->getError());
                exit;
            }else{
                $this->success('编辑成功');
                exit;
            }
        }

        // 处理地区数据回显
        $row['regionCode'] = $row['district'] ?: ($row['city'] ?: $row['province']);

        $this->assign([
            "row" => $row
        ]);
        
        return $this->view->fetch();
    }

    public function del($ids=NULL){
        $list = $this->model->select($ids);

        if(!$list){
            $this->error(__('No Results were found'));
            exit;
        }

        $result = $this->model->where(['id'=>['IN',$ids]])->delete(true);
        
        if($result === FALSE){
            $this->error('删除失败');
            exit;
        }else{
            $this->success('删除成功');
            exit;
        }
    }
}
